﻿using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using System.Globalization;
using System.Data;


namespace Portal.WebParts
{
    [ToolboxItemAttribute(false)]
    public partial class HappinessOffice : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public HappinessOffice()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            SPList happinessOfficeList = web.GetList("/Lists/HappinessOffice/");

            SPQuery happinessQuery = new SPQuery();

            happinessQuery.Query = @"<OrderBy>
                             <FieldRef Name='HappinessDate' Ascending='False' />
                            </OrderBy>";
            happinessQuery.ViewFields = @"  <FieldRef Name='ID'/>
                                            <FieldRef Name='Title'/>
                                            <FieldRef Name='Description'/>
                                            <FieldRef Name='HappinessImage'/>
                                            <FieldRef Name='HappinessDate'/>                                      
                                ";

            SPListItemCollection happinessItems = happinessOfficeList.GetItems(happinessQuery);

            if (happinessItems.Count > 0)
            {
                var happinessItemsModified = from happinessItem in happinessItems.GetDataTable().AsEnumerable()
                                             select new
                                             {
                                                 HappinessDate = "On " + Convert.ToDateTime(happinessItem["HappinessDate"].ToString()).ToString("MMM dd, yyyy"),
                                                 HappinessDescription = happinessItem["Description"].ToString().Length >= 300 ? happinessItem["Description"].ToString().Substring(0, 300) + "..." : happinessItem["Description"].ToString(),
                                                 HappinessTitle = happinessItem["Title"].ToString(),
                                                 HappinessID = happinessItem["ID"].ToString(),
                                                 HappinessImage = happinessItem["HappinessImage"].ToString()
                                             };

                lstHappinessOffice.DataSource = happinessItemsModified.ToList();
                lstHappinessOffice.DataBind();

            }

        }
    }
}
